/**

* <p>Title: UserServiceImpl.java</p>

* <p>Description: </p>

* <p>Copyright: Copyright (c) 2021</p>

* <p>Company: www.chinasofti.com</p>

* @author knowno

* @date 2021年3月11日

* @version 1.0

*/
package com.etc.zmall.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.etc.dao.UserMapper;
import com.etc.entity.User;
import com.etc.entity.UserExample;
import com.etc.util.MD5Util;
import com.etc.zmall.service.UserService;

/**
 * 
 * <p>
 * Title: UserServiceImpl
 * </p>
 * 
 * <p>
 * Description:
 * </p>
 * 
 * @author knowno
 * 
 * @date 2021年3月11日
 * 
 */
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;

	// 使用redisTemplate来完成缓存操作
	@Autowired
	private RedisTemplate redisTemplate;

	/**
	 * (non-Javadoc)
	 * 
	 * <p>
	 * Title: selectUsers
	 * </p>
	 * 
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param user
	 * @return
	 * 
	 * @see com.etc.zmall.service.UserService#selectUsers(com.etc.entity.User)
	 * 
	 */
	@Override
	public User selectUsers(User user) {

		// 1= >先去redis查
		Object obj = redisTemplate.opsForValue().get(user.getUsername());
		// 如果有数据 直接返回
		if (obj != null) {
			return (User) obj;
		}

		// 2 去myql查询的代码
		UserExample example = new UserExample();
		example.createCriteria().andUsernameEqualTo(user.getUsername())
				.andUserpwdEqualTo(MD5Util.getEncodeByMd5(user.getUserpwd()));

		List<User> list = userMapper.selectByExample(example);

		if (list.size() != 0) {
			// return list.get(0);
			// 查询mySql有数据，存放到redis中
			User userResult = list.get(0);
			redisTemplate.opsForValue().set(userResult.getUsername(), userResult);
			return userResult;
		}
		//如果redis mysql 都没有 就return null
		return null;
	}

}

